﻿using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using LitJson;

public class GenConfigClass  
{
	public class Field
	{
        public string type;
		public string name;
        public Field (string type, string name)
        {
        	this.name = name;
        	this.type = type;
        }
	}

	public static string savePath = "Assets/Client/Scripts/DataModel/";


	// 生成json类
	public static void CreateJsonClass (string path)
	{
		TextAsset asset = AssetDatabase.LoadAssetAtPath(path, typeof(TextAsset)) as TextAsset;
		string context = asset.text;
		string[] jsonNameArray = path.Split('/');
		string jsonName = jsonNameArray[jsonNameArray.Length - 1];
		jsonName = jsonName.Split('.')[0];

		Debug.Log("jsonName == " + jsonName);
		Debug.Log("context == " + context);
		StringBuilder builder = new StringBuilder();
		// 0. 生成注释
		builder.AppendLine("/* Auto Generated Code */");
        
		// 1. 创建类头
		builder.AppendLine("using System.Collections.Generic;");
        builder.AppendLine("namespace Config {");
		builder.AppendFormat("public class {0}\r\n{{", jsonName);
        builder.AppendLine();

		// 2. 读取json
		JsonData jdata = JsonMapper.ToObject(context);
		Gen(jsonName, jdata, ref builder);

		// 3. 创建
		builder.AppendLine();
		builder.AppendLine("}");
        builder.AppendLine("}");
		// 4. 保存
		using (FileStream file = new FileStream(savePath + jsonName + ".cs", FileMode.Create, FileAccess.Write))
        {
            using (TextWriter writer = new StreamWriter(file, Encoding.UTF8))
            {
                writer.Write(builder.ToString());
            }
        }
	}

	public static void Gen (string name, JsonData jdata, ref StringBuilder builder)
	{
		if (jdata.IsArray)
		{	
			string className = "C" + System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(name);
			builder.AppendFormat("	public class {0}Item\r\n    {{", className);
			builder.AppendLine();
			builder.AppendLine();	

			for (int i = 0; i < jdata.Count; i ++)
			{
				JsonData child = jdata[i];
				foreach (string key in child.Keys)
				{
					if (child[key].IsBoolean)
					{
						builder.AppendFormat("		public bool {0};\r\n", key);
					}
					else if (child[key].IsReal)
					{
						builder.AppendFormat("		public float {0};\r\n", key);
					}
					else if (child[key].IsNatural)
					{
						builder.AppendFormat("		public int {0};\r\n", key);
					}
					else if (child[key].IsString)
					{
						builder.AppendFormat("		public string {0};\r\n", key);
					}
					else if (child[key].IsObject)
					{
						Gen(key, child[key], ref builder);
					}
					else if (child[key].IsArray)
					{
						Gen(key, child[key], ref builder);
					}
				}
				break;
			}

			builder.AppendLine();
			builder.AppendLine("	}");
			builder.AppendFormat("	public List<{0}Item> {1};\r\n", className, name + "List");
		}
		else if (jdata.IsObject)
		{
			foreach (string key in jdata.Keys)
			{
				JsonData val = jdata[key];
				if (val.IsBoolean)
				{
					builder.AppendFormat("		public bool {0};\r\n", key);
				}
				else if (val.IsReal)
				{
					builder.AppendFormat("		public float {0};\r\n", key);
				}
				else if (val.IsNatural)
				{
					builder.AppendFormat("		public int {0};\r\n", key);
				}
				else if (val.IsString)
				{
					builder.AppendFormat("		public string {0};\r\n", key);
				}
				else if (val.IsArray)
				{
					Gen(key, val, ref builder);
				}
				else if (val.IsObject)
				{
					string className = "C" + System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(key);

					builder.AppendFormat("	public class {0}\r\n    {{", className);
					builder.AppendLine();
					builder.AppendLine();

					Gen(key, val, ref builder);

					builder.AppendLine();
					builder.AppendLine("	}");
					builder.AppendFormat("	public {0} {1};\r\n", className, key);
				}
			}
		}
	}
}
